import { Component, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { STComponent, STColumn, STRequestOptions, STChange, STData } from '@delon/abc/st';
import { SFComponent, SFSchema, SFDateWidgetSchema } from '@delon/form';
import { SearchDrawerService } from '@shared';
import { NzModalService } from 'ng-zorro-antd/modal';
import { BasicTableComponent } from 'src/app/routes/commom';
import { FreightAccountService } from '../../services/freight-account.service';

@Component({
  selector: 'app-receipt-order',
  templateUrl: './receipt-order.component.html',
  styleUrls: ['../../../commom/less/commom-table.less']
})
export class ReceiptOrderComponent extends BasicTableComponent implements OnInit {
  columns: STColumn[] = this.initST();
  schema: SFSchema = this.initSF();

  selectedRows: any[] = [];
  constructor(
    public service: FreightAccountService,
    private nzModalService: NzModalService,
    private router: Router,
    public searchDrawerService: SearchDrawerService
  ) {
    super(searchDrawerService);
  }

  ngOnInit(): void {}

  search() {
    this.st?.load(1);
  }

  beforeReq = (requestOptions: STRequestOptions) => {
    if (this.sf) {
      Object.assign(requestOptions.body, {
        ...this.sf?.value
      });
      if (this.sf?.value.brmdate) {
        Object.assign(requestOptions.body, {
          brmdate: {
            start: this.sf?.value.brmdate?.[0] || '',
            end: this.sf?.value.brmdate?.[1] || ''
          }
        });
      }
      if (this.sf?.value.createTime) {
        Object.assign(requestOptions.body, {
          createTime: {
            start: this.sf?.value.createTime?.[0] || '',
            end: this.sf?.value.createTime?.[1] || ''
          }
        });
      }
    }
    this.selectedRows = [];
    return requestOptions;
  };

  stChange(e: STChange): void {
    switch (e.type) {
      case 'checkbox':
        this.selectedRows = e.checkbox!;
        break;
    }
  }

  addInvoice() {
    if (this.selectedRows?.length <= 0) {
      this.service.msgSrv.warning('请选择收款单');
      return;
    }
    // const modal = this.nzModalService.create({
    //   nzTitle: '收款单',
    //   nzContent: AddCollectionInvoiceModalComponent,
    //   nzComponentParams: { i: { userId: 0 } },
    //   nzFooter: null
    // });
  }

  exportList() {
    this.service.exportStart({ ...this.sf?.value, pageSize: -1 }, this.service.$api_get_receipt_page_export);
  }

  private initSF(): SFSchema {
    return {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true
          }
        },
        brmcode: {
          type: 'string',
          title: '收款单号',
          ui: {
            placeholder: '请输入'
          }
        },
        ltdId: {
          type: 'string',
          title: '网络货运人',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getNetworkFreightForwarder()
          },
          default: null
        },
        bankreceipt: {
          type: 'string',
          title: '银行水单',
          ui: {
            placeholder: '请输入'
          }
        },
        brmtype: {
          type: 'string',
          title: '收款类型',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'driverrecord:receive:type' },
            placeholder: '请选择'
          }
        },
        brmmode: {
          type: 'string',
          title: '收款方式',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'receive:mode' },
            placeholder: '请选择'
          }
        },
        arto: {
          type: 'string',
          title: '付款人',
          ui: {
            widget: 'select',
            serverSearch: true,
            searchDebounceTime: 300,
            searchLoadingText: '搜索中...',
            allowClear: true,
            onSearch: (q: any) => this.service.getEnterpriceList({ enterpriseName: q })
          }
        },
        sts: {
          type: 'string',
          title: '收款状态',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'write:off:status' },
            placeholder: '请选择'
          }
        },
        brmdate: {
          title: '到账日期',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd'
          } as SFDateWidgetSchema
        },
        createTime: {
          title: '创建时间',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd'
          } as SFDateWidgetSchema
        },
        // billHCode: {
        //   type: 'string',
        //   title: '订单号',
        //   ui: {
        //     placeholder: '请输入',
        //     visibleIf: {
        //       expand: (value: boolean) => value
        //     }
        //   }
        // },
        // feeHCodes: {
        //   type: 'string',
        //   title: '费用号',
        //   ui: {
        //     placeholder: '请输入',
        //     visibleIf: {
        //       expand: (value: boolean) => value
        //     }
        //   }
        // },
        remarks: {
          type: 'string',
          title: '收款备注',
          ui: {
            placeholder: '请输入'
          }
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '', index: 'key', type: 'checkbox', width: 60, fixed: 'left', className: 'text-center' },
      {
        title: '序号',
        className: 'text-center',
        width: '60px',
        format: (item: STData, col: STColumn, idx: number) => {
          return ((this.st.pi -1) * this.st.ps) + idx + 1 + ''
        } 
      },
      { title: '收款单号', index: 'brmcode', type: 'link', width: 180, className: 'text-left' },
      // { title: '订单号', index: 'billHCode',  width: 180, className: 'text-left' },
      // { title: '费用号', index: 'feeCode',  width: 180, className: 'text-left' },
      { title: '网络货运人', index: 'ltdName', width: 200, className: 'text-left' },
      { title: '到账日期', index: 'brmdate', type: 'date', width: 200, className: 'text-left' },
      { title: '收款账户', index: 'ltdaccountCode', width: 200, className: 'text-left' },
      {
        title: '到账金额',
        index: 'brmmoney',
        width: 200,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.brmmoney }) }
      },
      {
        title: '核销金额',
        index: 'brmmoney',
        width: 200,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.brmmoney }) }
      },
      {
        title: '预收金额',
        index: 'yskmoney',
        width: 200,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.yskmoney }) }
      },
      { title: '银行类型', index: 'banktypeLabel', width: 200, className: 'text-left' },
      { title: '收款类型', index: 'brmtypeLabel', width: 150, className: 'text-left' },
      { title: '收款方式', index: 'brmModeLabel', width: 150, className: 'text-left' },
      { title: '付款人', index: 'artoname', width: 200, className: 'text-left' },
      { title: '银行水单', index: 'bankreceipt', width: 200, className: 'text-left' },
      { title: '创建时间', index: 'createTime', type: 'date', width: 200, className: 'text-left' },
      // { title: '创建人', index: 'createUserName', width: 150, className: 'text-left' },
      { title: '收款状态', index: 'stsLabel', width: 200, className: 'text-left' },
      { title: '收款备注', index: 'remarks', width: 200, className: 'text-left' },
      {
        title: '操作',
        width: '110px',
        fixed: 'right',
        className: 'text-center',
        buttons: [
          {
            text: '浏览',
            acl: { ability: ['FINANCIAL-RECEIPT-view'] },
            click: item => this.router.navigate(['/financial-management/receipt-order/detail/' + item.id])
          }
          // {
          //   text: '核销'
          // }
        ]
      }
    ];
  }
}
